home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_09_04 / 9n04086b < prev    next >
Text File  |  1991-02-04  |  5KB  |  145 lines

  1.  
  2.  
  3. /* *************************************************************
  4.      DSAT.C         Dynamic String Array Test
  5.      Version 1.0
  6.      Created by:    Anthony Whitford
  7.      (c) 1990
  8.      ********************************************************* */
  9.  
  10. #include <process.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14.  
  15. typedef char **     ptrarray_t;
  16.  
  17. int wordcount (const char *);
  18. int str_to_ptrarray (const char *, char ***);
  19. void free_ptrarray (char ***);
  20.  
  21. void main (void)
  22.      {
  23.      int numofitems, retval;
  24.      ptrarray_t strlist;
  25.      if ((retval = str_to_ptrarray("This is a test.", &strlist)) == -1)
  26.       printf("Error turning the string into a pointer list.");
  27.      else
  28.       {
  29.       /* Test the data. */
  30.           printf("\nString list outside function...\n");
  31.       for (numofitems = 0; strlist[numofitems] !=NULL; numofitems++)
  32.            {
  33.            printf("%u -->   ***%s***\n",
  34.            numofitems, strlist[numofitems]);
  35.            }
  36.           puts("");
  37.       free_ptrarray(&strlist); /* Free pointer array. */
  38.           }
  39.      exit (retval);      /* Return number of words or error. */
  40.      }
  41.  
  42. /* ****************************************************************          
  43.      Function:      wordcount
  44.      Parameters:    const char *str     -> the string.
  45.      Description:   Counts the number of words in a string up to 
  46.                          255 bytes.
  47.      Returns:       The number of words in astring.
  48.      ************************************************************* */
  49.  
  50. int wordcount (const char *str)
  51.      {
  52.      int words = 1;           /* Initialize word cunt. */
  53.      char tstr[256];          /* Declare a temporary string. */
  54.      strcpy(tstr, str);       /* Make a working copy. */
  55.      if (strtok(tstr, " ")  == NULL)    /* If string is only */
  56.       return (0);                        /*  spaces, return 0. */
  57.      while (strtok(NULL, " ") !=NULL)
  58.      words++;            /* Count how many words. */
  59.      return (words);          /* Return number of words in string. */
  60.      }
  61.  
  62. /*  ************************************************************
  63.      Function:      str_to_ptrarray
  64.      Parameters:    const char *orgstr  -> the original string 
  65.                                                   to be parsed,
  66.                     char ***ptrarray    -> the address ofthe 
  67.                                              pointer array.
  68.      Description:   Breaks a string into its words and composes
  69.                          a pointer array of the words.
  70.      Returns:       "ptrarray" becomes a NULL terminating pointer
  71.                     array, and pointer array, or -1 if an error 
  72.                     occurred.
  73.      ******************************************************** */
  74.  
  75. int str_to_ptrarray (const char *orgstr, char ***ptrarray)
  76.      {
  77.      unsigned index = 0, words;
  78.      char *cptr,*thestr; 
  79.      if (( thestr = (char *) calloc((size_t)(strlen(orgstr) + 1),
  80.           sizeof(*thestr))) == NULL)
  81.       return (-1);        /* Make a working copy of the
  82.                    string. */
  83.      strcpy(thestr, orgstr);
  84.      words = wordcount(thestr);    /* Get number of words. */
  85.      if ((*ptrarray = (char **) calloc(words + 1,
  86.        (size_t) sizeof(**ptrarray))) ==NULL)
  87.        {
  88.        free(thestr);
  89.        return (-1);        /* Error allocating space for
  90.                    ptrarray.*/
  91.        }
  92.  
  93.      /* THIS IS A NEW PRINTF */
  94.      for (index = 0; index < words; index ++)
  95.     {
  96.     printf("\n & *ptrarray[index] %x & (*ptrarray)[index] %x",
  97.         ptrarray[index], &((*ptrarray)[index]));
  98.     }
  99.      index = 0;
  100.  
  101.      cptr = strtok(thestr, " ");
  102.      while (index < words)
  103.       {
  104.       if ((*ptrarray[index] = (char *)calloc((size_t)(strlen(cptr) + 1),
  105.            sizeof(**ptrarray[index]))) == NULL)
  106.             {
  107.                     free_ptrarray(ptrarray);
  108.             free(thestr);  /* If memory allocation
  109.                          error, free */
  110.             return (-1);   /* thestr, ptrarray, and
  111.                           return -1. */
  112.             }
  113.       strcpy(*ptrarray[index++], cptr);
  114.       cptr = strtok (NULL, " ");
  115.       }
  116.      *ptrarray[index] = NULL; /* Terminate ptrarray with a NULL. */
  117.      free(thestr);            /* Free allocated memory. */
  118.      /* Test the data. --- Delete the next four lines when
  119.                     debugged!!  */
  120.      printf("\nString list inside function...\n");
  121.      for (index = 0; *ptrarray[index] != NULL; index++)
  122.       printf("%u --> ***%s***\n", index, *ptrarray[index]);
  123.      puts(" ");
  124.      
  125.      return (words);     /* Return the number of pointers. */
  126.      }
  127.  
  128. /* ************************************************************
  129.      Function:      free_ptrarray
  130.      Parameters:    char ***ptrarray  _> address ofthe pointer 
  131.                                                   array.
  132.      Description:   Frees allocated memory for the pointer array 
  133.                     allocated with the str_to_ptrarray function.
  134.      Returns:       Nothing.
  135.      ******************************************************* */
  136. void free_ptrarray (char ***ptrarray)
  137.      {
  138.      unsigned count = 0; /* Declare and reset counter. */
  139.      for (; *ptrarray[count] != NULL; count++)
  140.       free(*ptrarray[count]);  /* Free each string/word. */
  141.      free(*ptrarray);              /* Free the handle. */
  142.      }
  143.  
  144.  
  145.